import { defineStore } from 'pinia'
import { reqCategory1, reqCategory2, reqCategory3, reqAttrInfoList } from '@/api/product/attr/'
import type { ICategory, IAttrValue } from '@/api/product/attr/types'

export const useCategoryStore = defineStore('Category', () => {
  const category1Arr = ref<ICategory[]>([])
  const category2Arr = ref<ICategory[]>([])
  const category3Arr = ref<ICategory[]>([])

  const attrInfoArr = ref<IAttrValue[]>([])

  const cate1Id = ref<number | string>('')
  const cate2Id = ref<number | string>('')
  const cate3Id = ref<number | string>('')

  const getCategory1 = async () => {
    const result = await reqCategory1()
    if (result.code == 200) {
      category1Arr.value = result.data
    }
  }
  const getCategory2 = async () => {
    const result = await reqCategory2(cate1Id.value as number)
    if (result.code == 200) {
      category2Arr.value = result.data
    }
  }
  const getCategory3 = async () => {
    const result = await reqCategory3(cate2Id.value as number)
    if (result.code == 200) {
      category3Arr.value = result.data
    }
  }
  const getAttrInfoList = async () => {
    const result = await reqAttrInfoList(cate1Id.value as number, cate2Id.value as number, cate3Id.value as number)
    if (result.code == 200) {
      attrInfoArr.value = result.data
    }
  }
  return {
    category1Arr,
    category2Arr,
    category3Arr,
    attrInfoArr,
    cate1Id,
    cate2Id,
    cate3Id,
    getCategory1,
    getCategory2,
    getCategory3,
    getAttrInfoList
  }
})
